package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.ResearchkeywordDAO;

import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Researchkeyword;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Researchkeyword entities
 * 
 */

@Service("ResearchkeywordService")
@Transactional
public class ResearchkeywordServiceImpl implements ResearchkeywordService {

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Researchkeyword entities
	 * 
	 */
	@Autowired
	private ResearchkeywordDAO researchkeywordDAO;

	/**
	 * Instantiates a new ResearchkeywordServiceImpl.
	 *
	 */
	public ResearchkeywordServiceImpl() {
	}

	/**
	 * Delete an existing Researchkeyword entity
	 * 
	 */
	@Transactional
	public void deleteResearchkeyword(Researchkeyword researchkeyword) {
		researchkeywordDAO.remove(researchkeyword);
		researchkeywordDAO.flush();
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Researchkeyword deleteResearchkeywordFacultyresearchs(Integer researchkeyword_researchKeywordId, Integer related_facultyresearchs_facultyResearchId) {
		Facultyresearch related_facultyresearchs = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs_facultyResearchId, -1, -1);

		Researchkeyword researchkeyword = researchkeywordDAO.findResearchkeywordByPrimaryKey(researchkeyword_researchKeywordId, -1, -1);

		related_facultyresearchs.getResearchkeywords().remove(researchkeyword);
		researchkeyword.getFacultyresearchs().remove(related_facultyresearchs);

		facultyresearchDAO.remove(related_facultyresearchs);
		facultyresearchDAO.flush();

		return researchkeyword;
	}

	/**
	 * Return a count of all Researchkeyword entity
	 * 
	 */
	@Transactional
	public Integer countResearchkeywords() {
		return ((Long) researchkeywordDAO.createQuerySingleResult("select count(o) from Researchkeyword o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Researchkeyword entity
	 * 
	 */
	@Transactional
	public void saveResearchkeyword(Researchkeyword researchkeyword) {
		Researchkeyword existingResearchkeyword = researchkeywordDAO.findResearchkeywordByPrimaryKey(researchkeyword.getResearchKeywordId());

		if (existingResearchkeyword != null) {
			if (existingResearchkeyword != researchkeyword) {
				existingResearchkeyword.setResearchKeywordId(researchkeyword.getResearchKeywordId());
				existingResearchkeyword.setKeyword(researchkeyword.getKeyword());
			}
			researchkeyword = researchkeywordDAO.store(existingResearchkeyword);
		} else {
			researchkeyword = researchkeywordDAO.store(researchkeyword);
		}
		researchkeywordDAO.flush();
	}

	/**
	 * Return all Researchkeyword entity
	 * 
	 */
	@Transactional
	public List<Researchkeyword> findAllResearchkeywords(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Researchkeyword>(researchkeywordDAO.findAllResearchkeywords(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Researchkeyword findResearchkeywordByPrimaryKey(Integer researchKeywordId) {
		return researchkeywordDAO.findResearchkeywordByPrimaryKey(researchKeywordId);
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Researchkeyword saveResearchkeywordFacultyresearchs(Integer researchKeywordId, Facultyresearch related_facultyresearchs) {
		Researchkeyword researchkeyword = researchkeywordDAO.findResearchkeywordByPrimaryKey(researchKeywordId, -1, -1);
		Facultyresearch existingfacultyresearchs = facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs.getFacultyResearchId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearchs != null) {
			existingfacultyresearchs.setFacultyResearchId(related_facultyresearchs.getFacultyResearchId());
			existingfacultyresearchs.setTitle(related_facultyresearchs.getTitle());
			existingfacultyresearchs.setResearchDescription(related_facultyresearchs.getResearchDescription());
			existingfacultyresearchs.setResearchStatus(related_facultyresearchs.getResearchStatus());
			existingfacultyresearchs.setMinumumRequirements(related_facultyresearchs.getMinumumRequirements());
			related_facultyresearchs = existingfacultyresearchs;
		} else {
			related_facultyresearchs = facultyresearchDAO.store(related_facultyresearchs);
			facultyresearchDAO.flush();
		}

		related_facultyresearchs.getResearchkeywords().add(researchkeyword);
		researchkeyword.getFacultyresearchs().add(related_facultyresearchs);
		related_facultyresearchs = facultyresearchDAO.store(related_facultyresearchs);
		facultyresearchDAO.flush();

		researchkeyword = researchkeywordDAO.store(researchkeyword);
		researchkeywordDAO.flush();

		return researchkeyword;
	}

	/**
	 * Load an existing Researchkeyword entity
	 * 
	 */
	@Transactional
	public Set<Researchkeyword> loadResearchkeywords() {
		return researchkeywordDAO.findAllResearchkeywords();
	}
}
